package com.higgins.eventtracking.client.health.components;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.TrackingEventDTO;
import com.higgins.eventtracking.client.health.fundamentals.BodySubTab;

public class LastBodyStatsLoader implements StatsLoader {
	private final EventTrackingServiceAsync greetingService;
	private BodySubTab parent;
	private int columnIndex;
	private int rowIndex;
	private String text;
	private String[] distanceSports = {"Swimming", "Walking", "Running", "Cycling"};
	private List<String> distanceSportsList = Arrays.asList(distanceSports);

	
	public LastBodyStatsLoader(EventTrackingServiceAsync greetingService, BodySubTab parent, int columnIndex, int rowIndex, String text)	{
		this.parent = parent;
		this.columnIndex = columnIndex;
		this.rowIndex = rowIndex;
		this.text = text;
		this.greetingService = greetingService;
	}
	
	public void execute()	{
		if (distanceSportsList.contains(text))		{
			greetingService.retrieveTrackingEvent(text, new AsyncCallback<TrackingEventDTO>() {
				@Override
				public void onFailure(Throwable caught) {
				}
	
				@Override
				public void onSuccess(TrackingEventDTO result) {
					if (result!=null && result.getDistance()!=null)	{
						parent.populateField(columnIndex, rowIndex, result.getDistance() > 0.0 ? "" + result.getDistance() + " Km ":"");
					}
				}
			});
		} else {
			greetingService.retrieveTrackingEvent(text, new AsyncCallback<TrackingEventDTO>() {
				@Override
				public void onFailure(Throwable caught) {
				}
	
				@Override
				public void onSuccess(TrackingEventDTO result) {
					if (result!=null && result.getWeight()!=null)	{
						parent.populateField(columnIndex, rowIndex, result.getWeight() > 0.0 ? "" + result.getWeight() + " Kg":"");
					}
				}
			});
		}
	}

}
